<?php 
//if(!isset($_SESSION)) session_start();
global $info_bar_data;
global $article;


$info_bar_file = $_SERVER['DOCUMENT_ROOT'] ."/modules/info_bar/info_bar_data.txt";
//$pending_info_bar_file = $_SERVER['DOCUMENT_ROOT'] ."/modules/info_bar/pending_info_bar_data.txt";


if (file_exists($info_bar_file))  {
	$info_bar_data = unserialize (file_get_contents($info_bar_file));
	
	$info_bar_data["search"]["search"]["search.html"][1]["entry"] = "search";
	$info_bar_data["search"]["search"]["search.html"][1]["published"] = "1";
    //$info_bar_data["search"]["search"]["search.html"][2] =  "tasters";
       
}
else $info_bar_data = array ();
//if (file_exists($pending_info_bar_file))  $pending_info_bar_data = unserialize (file_get_contents($pending_info_bar_file));
//else $pending_info_bar_data = array ();


function has_info ($data, $section, $area, $file) {
    if (isset($data[$section])) {
        if (isset($data[$section][$area])) {
            if (isset ($data[$section][$area][$file])) return true;
            else return false;
        } else return false;
    } else return false;
}

function save_info_file () {
				 global $info_bar_file;
				 global $info_bar_data;
				 $info_bar_data_string = serialize ($info_bar_data);
				 file_put_contents ($info_bar_file ,$info_bar_data_string);
}

function do_info_update ($area, $section, $file, $entry, $add) {
  				 global $info_bar_data;
					 if ($add == "true") {
					 	 $index = new_info_index ($area, $section, $file);
						 $info_bar_data[$area][$section][$file][$index]["entry"] = $entry;
						 $info_bar_data[$area][$section][$file][$index]["published"] = 0;			 
					 } else if ($add == "false") {
					 	 if (has_info($info_bar_data, $area, $section, $file)) {
        			 foreach ($info_bar_data[$area][$section][$file] as  $key => $info) {
            	 				 if ($info["entry"] == $entry) {
                			 		$info_bar_data[$area][$section][$file][$key]["delete"] = 1;
            					}
        				}
    					}
					 }
					 save_info_file (); 
}

function new_info_index ($area, $section, $file) {
				 global $info_bar_data;
				 if (has_info($info_bar_data, $area, $section, $file)) {
         		foreach ($info_bar_data[$area][$section][$file] as $key => $info) {
									$index = $key;
						}
        	  $index++;
				} else $index = 0;
				return $index;
}


function show_remove_box ($area, $section, $file) {
      global $info_bar_data;
			$remove_box = array();
			if (has_info($info_bar_data, $area, $section, $file)) {
	 			 foreach ($info_bar_data[$area][$section][$file] as $info) {
				   if (isset($info["published"])) $pending = "(pending)";
					 else $pending="";
					 //$pending="";
					 if (! isset($info["delete"])) {
	 				 		echo "<div id=info_remove_box><label for=\"{$info}\">{$info["entry"]} {$pending}</label><input type=\"checkbox\" checked ";
					 		echo " onclick=\"javascript:admin_save_info_settings ('{$area}','{$section}','{$file}',this.checked,'{$info["entry"]}');\" ></div>";
							$remove_box[] = $info["entry"];
					 }
	 			 }
			}	 
			return $remove_box;
}

function check_info_exists ($data,$area, $section, $file) {
    if (isset($data[$area])) {
        if (isset($data[$area][$section])) {
            if (isset ($data[$area][$section][$file])) return $data[$area][$section][$file];
            else return $data[$area][$section]["index.html"];
        } else return $data[$area];
    } else return false;
}

function alert ($info) {
    return "<script type='text/javascript'>alert('{$info}')</script>";
}

function show_info_bar($area, $section, $file) {
    global $info_bar_data;
    if ($file == "") {
    global $article;
    $file = basename ($article);
    }
    $text = "";
    if ($file=="index.html") $file = $area;
    if (has_info($info_bar_data, $area, $section, $file)) {
    $page_info = check_info_exists ($info_bar_data,$area, $section, $file);    
      
        foreach ($page_info as $i) {
				    //if ((! isset($i["published"]))|| ((isset($_SESSION['username']))&&(! isset($i["delete"])))  ) {
						//if ((! isset($i["published"]))|| ((isset($_SESSION['username']))&&(! isset($i["delete"])))  ) {
						if (((!isset($_SESSION['username'])) && isset($i["published"]) && $i["published"] == 1) ||
							 	((isset($_SESSION['username']))&&(! isset($i["delete"]))))
						{
            	 $text.= "<div>";
            	 $text.= file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/modules/info_bar/content/" . $i["entry"] . ".html");
            	 $text.= "</div>";
						}
        }

       
    } 
		if ($text != "") {
		 $text = "<div class=\"r_6_75 c_12_25 w_3_5\" id=info_bar>{$text}</div>";
		}
		//$text= "<div class=\"r_6_75 c_12_25 w_3_5\" id=info_bar>x</div>";
    return $text;
}

function show_info_select($remove_box,$area,$section,$file) {
global $info_bar_data;
$d = dir($_SERVER['DOCUMENT_ROOT'] . "/modules/info_bar/content");
echo "<div id=select_info><select onchange=\"javascript:admin_save_info_settings ('{$area}','{$section}','{$file}', true, this.value);\">";
echo "<option value=0>Select item....";
while (false !== ($entry = $d->read())) {
      if ($entry != '..' && $entry != '.') {
				 $entry = str_replace (".html","",$entry);
				 if (has_info($info_bar_data, $area, $section, $file)) {
				 		if (! in_array($entry,$remove_box)) {

							 echo "<option>{$entry}";
						}
				 } else echo "<option>{$entry}";
			}
}
echo "</select></div>";
}


function add_pending ($data, $section, $area, $file, $info, $action) {
				 
}


?>